﻿using MetroDataApi.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace MetroDataApi.Controllers.Management
{
    public class ExtraProjectInfoController : ApiController
    {
        // POST api/extraprojectinfo
        public void Post([FromBody]string value)
        {
            JObject jsonData = JObject.Parse(value);
            var extraInfo= JsonConvert.DeserializeObject<MonitorProjectExtraInfo>(value);
            var p = jsonData["property"].ToString();
            using (var db = new MetroDataDataContext())
            {
                var existed = db.MonitorProjectExtraInfo.SingleOrDefault(q => q.PrjId == extraInfo.PrjId);
                if (existed != null)
                {
                    //反射得到属性
                    var type = typeof(MonitorProjectExtraInfo);
                    var propertyInfo = type.GetProperty(p);
                    var newValue = propertyInfo.GetValue(extraInfo);
                    propertyInfo.SetValue(existed, newValue);

                }
                else
                {
                    db.MonitorProjectExtraInfo.InsertOnSubmit(extraInfo);
                }
                db.SubmitChanges();
            }
        }
    }
}
